<template>
  <!-- 当 visible 为 true 时显示弹窗 -->
  <div v-if="visible" class="user-modal">
    <div class="user-modal-overlay" @click="handleClose"></div>
    <div class="user-modal-content">
      <div class="user-modal-header">
        <h3>{{ mode === "view" ? "用户详情" : "编辑用户" }}</h3>
        <button class="close-btn" @click="handleClose">&times;</button>
      </div>
      <div class="user-modal-body">
        <!-- 用户详情展示 -->
        <div v-if="mode === 'view'">
          <div class="detail-item">
            <label>头像：</label>
            <a-avatar :size="50" class="user-avatar">
              <img v-if="user.userAvatar" :src="user.userAvatar" alt="avatar" />
              <icon-user v-else class="default-avatar" />
            </a-avatar>
          </div>
          <div class="detail-item">
            <label>用户名：</label>
            <span>{{ user.userName }}</span>
          </div>
          <div class="detail-item">
            <label>账号：</label>
            <span>{{ user.userAccount }}</span>
          </div>
          <div class="detail-item">
            <label>个人简介：</label>
            <span>{{ user.userProfile || "暂无个人介绍" }}</span>
          </div>
          <div class="detail-item">
            <label>角色：</label>
            <span>{{ user.userRole }}</span>
          </div>
          <div class="detail-item">
            <label>创建时间：</label>
            <span>{{ formattedDate }}</span>
          </div>
        </div>
        <!-- 编辑用户表单 -->
        <div v-else>
          <form class="user-edit-form" @submit.prevent="submitEdit">
            <div class="form-item">
              <label>用户名：</label>
              <input
                type="text"
                v-model="editUser.userName"
                placeholder="请输入用户名"
                required
              />
            </div>
            <div class="form-item">
              <label>个人简介：</label>
              <textarea
                v-model="editUser.userProfile"
                placeholder="请输入个人简介"
              ></textarea>
            </div>
            <a-button
              type="outline"
              @click="resetPassword"
              style="width: 100px; align-self: flex-end"
              >重置密码</a-button
            >
            <!-- 根据实际需求可继续扩展其它编辑项 -->
            <div class="form-buttons">
              <button type="button" class="cancel-btn" @click="handleClose">
                取消
              </button>
              <button type="submit" class="save-btn">保存</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// 该组件既可用于显示用户详情又可用于编辑用户信息
import { defineProps, defineEmits, ref, watch, computed } from "vue";
import moment from "moment";
import { IconUser } from "@arco-design/web-vue/es/icon";
import { Message } from "@arco-design/web-vue";
import { Avatar } from "@arco-design/web-vue";
import { UserControllerService } from "../../generated";
// 定义用户数据接口
interface User {
  createTime: string;
  userAvatar?: string;
  userName: string;
  userAccount?: string;
  userProfile?: string;
  userRole: string;
  userId: string;
}

// 定义组件 props
const props = defineProps<{
  visible: boolean;
  mode: "view" | "edit";
  user: User;
}>();

// 定义组件 emit 事件
const emit = defineEmits<{
  (e: "close"): void;
  (e: "save", updatedUser: User): void;
}>();

// 当为编辑状态时，使用一个拷贝以便修改
const editUser = ref({ ...props.user });

// 监听传入的用户数据变化，及时更新编辑对象
watch(
  () => props.user,
  (newUser) => {
    editUser.value = { ...newUser };
    console.log("editUser", editUser.value);
  },
  { immediate: true }
);

// 关闭弹窗
const handleClose = () => {
  emit("close");
};

// 保存编辑后的数据
const submitEdit = () => {
  emit("save", editUser.value);
};

// 格式化创建时间
const formattedDate = computed(() =>
  props.user.createTime
    ? moment(props.user.createTime).format("YYYY-MM-DD HH:mm")
    : ""
);

// 重置密码
const resetPassword = async () => {
  console.log("重置密码");
  console.log(props.user.userId);
  const res = await UserControllerService.resetPasswordForcedUsingPost(
    props.user.userId
  );
  if (res.data) {
    Message.success("密码重置成功，密码为123456");
  }
  console.log(res);
};
</script>

<style scoped>
.user-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}

.user-modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
}

.user-modal-content {
  position: relative;
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  width: 500px;
  z-index: 1000;
  animation: slideDown 0.3s ease-out;
}

@keyframes slideDown {
  from {
    transform: translateY(-20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.user-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #eee;
  padding-bottom: 10px;
  margin-bottom: 20px;
}

.user-modal-header h3 {
  margin: 0;
  font-size: 20px;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #999;
  transition: color 0.2s ease;
}

.close-btn:hover {
  color: #333;
}

.user-modal-body {
  max-height: 400px;
  overflow-y: auto;
}

/* 展示模式样式 */
.detail-item {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  font-size: 14px;
  color: #333;
}

.detail-item label {
  width: 100px;
  font-weight: 600;
}

.user-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}

.default-avatar {
  font-size: 50px;
  color: #ccc;
}

/* 编辑表单样式 */
.user-edit-form {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.form-item {
  display: flex;
  align-items: center;
}

.form-item label {
  width: 100px;
  font-weight: 600;
}

input[type="text"],
textarea {
  flex: 1;
  padding: 8px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 14px;
}

textarea {
  resize: vertical;
  min-height: 80px;
}

.form-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

.cancel-btn,
.save-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.cancel-btn {
  background: #f5f5f5;
  color: #333;
}

.cancel-btn:hover {
  background: #e6e6e6;
}

.save-btn {
  background: #1890ff;
  color: white;
}

.save-btn:hover {
  background: #147ad6;
}
</style>
